<%@page import="java.util.ArrayList"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    <%@ page import="servlets.*"%>
<%@ page import="org.hibernate.SessionFactory"%>
<%@ page import="org.hibernate.Session"%>
<%@ page import="java.util.List"%>
<%@ page import="hibernate.model.*"%>
<%@ page import="hibernate.util.HibernateUtil"%>
<%@ page import="hibernate.method.*"%>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<style type="text/css" media="screen">
/*====================================================
	- HTML Table Filter stylesheet
=====================================================*/
@import "../management/css/filtergrid.css";

/*====================================================
	- General html elements
=====================================================*/
body{ 
	margin:15px; padding:15px; border:1px solid #666;
	
}
h2{ margin-top: 50px; }
caption{ margin:10px 0 0 5px; padding:10px; text-align:left; }
pre{ font-size:13px; margin:5px; padding:5px; background-color:black; border:1px solid #ccc;  }
.mytable{
	width:95%; font-size:12px;
	border:1px solid #ccc;
	color: white;
}
div.tools{ margin:5px; }
div.tools input{ background-color:black; border:2px outset #f4f4f4; margin:2px; color:white;}
th{ background-color:#003366; color:#FFF; padding:2px; border:1px solid #ccc; }
td{ padding:2px; border-bottom:1px solid #ccc; border-right:1px solid #ccc; }

</style>
<script type="text/javascript" src="../management/js/jquery.tablesorter.js" > </script>
<script type="text/javascript" src="../management/js/actb.js"></script><!-- External script -->
<script type="text/javascript" src="../management/js/tablefilter.js"></script>
<script>
 

$("a.games").click(function() {
		$("#games_content").hide().fadeIn(1000).load("games/gameProfile.jsp?game_id="+ $(this).attr('id'));
		return false;
	});

$("a.locals").click(function() {
	$("#games_content").hide().fadeIn(1000).load("main/localProfile.jsp?local_id="+ $(this).attr('id'));
	return false;
});


</script>
<%
						SessionFactory sessionFactory = HibernateUtil
								.getSessionAnnotationFactory();
						Session s = sessionFactory.openSession();
						s.beginTransaction();
						GameMethods gm = new GameMethods();
							response.setContentType("text/html");
							List<Game> games = gm.listAllGames(sessionFactory, s);
							FeedbackMethods fb = new FeedbackMethods();
					%> 
<div class="page-header">
	<h2 class="page-title">Nasze gry</h2>
</div>
<div id="content">
<br />
	
<%
		if (games.size() > 0) {
			int counter =1;
		%>
		<table id="table6" class="mytable" >
		<thead>
	<tr>
		<th style="width: 20px;">Lp</th>
		<th style="width: 150px">Nazwa gry</th>
		<th style="width: 100px">Kategorie</th>
		<th style="width: 35px;">Ocena</th>
		<th style="width: 50px;">Komentarzy</th>
		<th> Lokal + ilosc</th>
	</tr>
	</thead>
	<%
	for (Game g1  : games) {
		int game_id = g1.getGame_id();
		String gameName = g1.getName();		%>
		
<tbody>
	<tr>
<td>
<%=counter%>
	</td>
	<td>
	<%out.println("<a class=\"games\" id=\""+ game_id + "\" href=\"#\">" + gameName + "</a>"); %>
	</td>
	<td>
	<%
													GameCategoryMethods gcat = new GameCategoryMethods();
													List<String> gamecategories = new ArrayList<String>();
													gamecategories = gcat.listCategoriesByGame(sessionFactory, s, game_id);
													if (gamecategories == null) out.println("Brak");
													else if (gamecategories.size() >0) {
													for (String gc : gamecategories){
														out.println(gc+", ");
													}
													}
													%>
	</td>
	<td>
	<%RateMethods rm = new RateMethods();
													out.println(rm.returnAverageRateByGame(sessionFactory, s, game_id)); 
													 %>
	</td>
	<td>
	<%
													int commentsCount = fb.countFbByGameId(sessionFactory, s, game_id);
													out.println(commentsCount); %>
	</td>
	<td>
	<%
	AssortmentMethods am = new AssortmentMethods();
	List <Local> loc = am.listLocalsByGame(sessionFactory, s, g1.getGame_id());
	if (loc == null || loc.size()==0 || loc.isEmpty()) out.println("Brak");
	else if (loc.size() >0) {
	for (Local gc : loc){
		out.println("<a class=\"locals\" id=\""+ gc.getLocal_id() + "\" href=\"#\">" +gc.getCity()+", "+ gc.getStreet()+" "+gc.getLocal_number()+" -> " +am.getItemsByLocalAndGame(sessionFactory, s, g1.getGame_id(), gc.getLocal_id()) + "</a>");
	}
	}
	%>
	</td>
	</tr>
	<%
	counter++;
	}
		}
		s.getTransaction().commit();		%>	
		
		</tbody>
</table>
<script>
$(document).ready(function() 
	    { 
	        $(".mytable").tablesorter({debug: true}); 
	    } 
	);
	</script>
<br>
<br>

		
<script type="text/javascript">
//<![CDATA[
	var table6_Props = 	{
							//paging: true,
							//paging_length: 3,
							rows_counter: true,
							btn_reset: true,
							loader: true,
							loader_text: "Filtering data..."
						};
	setFilterGrid( "table6",table6_Props );
//]]>
</script>
</div>	


		